package com.badou.project.rest.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.badou.designer.jdbc.common.util.HttpUtil;
import com.badou.plugins.cas.server.CasProperties;
import com.badou.project.rest.ReturnCodeEnum;
import com.badou.project.rest.vo.ApiReturnVO;
import com.badou.tools.common.PropertiesConfig;
import com.badou.tools.common.util.ParamUtils;


public class TokenInterceptor extends HandlerInterceptorAdapter{
	
	protected static Logger logger = Logger.getLogger(TokenInterceptor.class);
	
	/**
	 * 返回对象
	 */
	protected ApiReturnVO returnVO;
	 /**
     * preHandle方法是进行处理器拦截用的，顾名思义，该方法将在Controller处理之前进行调用，
     * SpringMVC中的Interceptor拦截器是链式的，可以同时存在多个Interceptor，
     * 然后SpringMVC会根据声明的前后顺序一个接一个的执行，
     * 而且所有的Interceptor中的preHandle方法都会在Controller方法调用之前调用。
     * SpringMVC的这种Interceptor链式结构也是可以进行中断的，
     * 这种中断方式是令preHandle的返回值为false，当preHandle的返回值为false的时候整个请求就结束了。
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    	String token = ParamUtils.getParameter(request, "access_token");
    	returnVO = new ApiReturnVO();
    	response.setCharacterEncoding("UTF-8");  
		response.setHeader("Content-Type", "text/html;charset=UTF-8");//这句话是解决乱码的  
    	if(StringUtils.isBlank(token))
    	{
    		returnVO.setReturnMsg(ReturnCodeEnum.FAIL.getReturnMsg()+"access_token不能为空");
			returnVO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
			response.getWriter().write(JSONObject.fromObject(returnVO).toString());  
    		return false;
    	}
    	String valiateToken =PropertiesConfig.getPropValue("token.valiate.url");
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("token", token);
    	JSONObject jsonResult =HttpUtil.httpPost(valiateToken, map, false);
    	if(jsonResult == null || !jsonResult.getBoolean("hasOk"))
    	{
    		if(jsonResult == null)
    		{
    			returnVO.setReturnMsg(ReturnCodeEnum.FAIL.getReturnMsg()+"access_token验证失败");
    		}
    		else
    			returnVO.setReturnMsg(ReturnCodeEnum.FAIL.getReturnMsg()+jsonResult.get("message"));
			returnVO.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
		
    		response.getWriter().write(JSONObject.fromObject(returnVO).toString());  
    		return false;
    	} 
        return true;
    }
	public ApiReturnVO getReturnVO() {
		return returnVO;
	}
	public void setReturnVO(ApiReturnVO returnVO) {
		this.returnVO = returnVO;
	}

   
	
}
